package com.alphawavesgames.engine.rendering;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author Duncan
 */
public class GLTextureManager extends TextureManager
{
    private Context m_context;
    private GLRenderHWInterface m_renderInterface;

    public GLTextureManager()
    {
        m_context = null;
        m_renderInterface = null;
    }

    public void initialise( GLRenderContainer renderContainer )
    {
        m_renderInterface = renderContainer.getRenderHWInterface();
        m_context = renderContainer.getContext();
    }

    @Override
    public void initialise( RenderContainer renderContainer )
    {
        initialise( (GLRenderContainer)renderContainer );
    }

    @Override
    public TextureGL getTexture( TextureHandle handle )
    {
        return ( TextureGL ) super.getTexture( handle );
    }    

    public TextureHandle createTexture( int assetID )
    {
        return add( new TextureGL( assetID ) );
    }

    /*public class TextureGL extends Texture
    {
        private int[] m_glTex;
        private int m_assetID;


        protected TextureGL( int assetID )
        {
            m_assetID = assetID;
            m_glTex = new int[1];
            m_glTex[0] = -1;

            m_pendingLoad = true;
        }

        @Override
        public void update()
        {
            if( m_pendingLoad )
            {
                load( m_assetID, m_context );
            }
        }

        //load the android specific drawable - doesn't really need loading
        public boolean load( int assetID, Context context )
        {
            GL10 gl = m_renderInterface.lockGL();

            boolean result = false;
            try
            {
                if( gl != null )
                {
                    InputStream is = context.getResources().openRawResource( assetID );
                    Bitmap bitmap = null;
                    try
                    {
                        //BitmapFactory is an Android graphics utility for images
                        bitmap = BitmapFactory.decodeStream(is);

                    }
                    finally
                    {
                        //Always clear and close
                        try
                        {
                            is.close();
                            is = null;
                        }
                        catch( IOException e )
                        {
                        }
                    }

                    //Generate one texture pointer...
                    gl.glGenTextures(1, m_glTex, 0);
                    //...and bind it to our array
                    gl.glBindTexture( GL10.GL_TEXTURE_2D, m_glTex[0] );

                    //Create Nearest Filtered Texture
                    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
                    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

                    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
                    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
                    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

                    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
                    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

                    //Clean up
                    bitmap.recycle();

                    m_pendingLoad = false;

                    result = true;
                }
                else
                {
                    //mark as pending to be loaded so we can try again with a valid gl device
                    m_pendingLoad = true;
                    result = false;
                }
            }
            finally
            {
                m_renderInterface.unlockGL();
            }

            return result;
        }

        public int getGLTex() { return m_glTex[0]; }
    }*/

}
